package my.app;

import my.Common.clsUtil;
import my.app.R;

import java.util.ArrayList;
import java.util.Calendar;

import my.BLL.clsDonHang;
import my.BLL.clsSQL;
import my.BLL.clsSanPham;

import my.DAL.clsDal;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;

public class frmMain extends Activity implements OnClickListener {
	// Controls
	private EditText m_SoDH;
	private EditText m_MaKH;
	private EditText m_TenKH;
	private EditText m_TongSL;
	private EditText m_TongTien;
	private Button m_btnNew;
	private Button m_btnInsert;
	private Button m_btnDelete;
	private Button m_btnUpdate;

	// List View
	private ListView m_lvwMain;
	private int m_intLastTouchAction = 1;
	private float m_dblLastLocY = 0;
	private float m_dblLastLocX = 0;
	private HorizontalScrollView m_hsvwListMain;
	private ScrollView m_svwListMain;
	private LinearLayout m_linListMain;
	private EditText m_txtFocusText;

	// Members
	private clsDal m_objDal;
	private clsDonHang m_objDonHang;
	private clsSanPham m_objSanPham;
	private clsSQL m_objSQL;
	private String m_MaSP = "";

	private ArrayList<clsSP> m_arlCollectDebt;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frmmain);

		LayoutInflater li;
		ViewGroup vg;

		m_btnNew = (Button) findViewById(R.id.btMoi_frmMain);
		m_btnNew.setOnClickListener(this);

		m_btnInsert = (Button) findViewById(R.id.btNhap_frmMain);
		m_btnInsert.setOnClickListener(this);

		m_btnDelete = (Button) findViewById(R.id.btXoa_frmMain);
		m_btnDelete.setOnClickListener(this);

		m_btnUpdate = (Button) findViewById(R.id.btSua_frmMain);
		m_btnUpdate.setOnClickListener(this);

		// ListView
		m_linListMain = (LinearLayout) findViewById(R.id.linList_frmMain);
		m_hsvwListMain = (HorizontalScrollView) findViewById(R.id.hsvw_frmMain);
		m_svwListMain = (ScrollView) findViewById(R.id.svw_frmMain);

		m_lvwMain = (ListView) findViewById(R.id.lvw_frmMain);
		// li = getLayoutInflater();
		// vg = (ViewGroup)li.inflate(R.layout.frmcollect_debt_grdrowheader,
		// m_lvwMain, false);
		// m_lvwMain.addHeaderView(vg);

		try {

			m_objDal = new clsDal(this);
			m_objSanPham = new clsSanPham(this, m_objDal);

			m_arlCollectDebt = new ArrayList<clsSP>();
			BindingGrid();
		} catch (Exception e) {
			clsUtil.ShowMsgErr(this, e.getMessage());
			this.finish();
		}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	// Private Methods
	private void BindingGrid() throws Exception {
		Cursor csr;
		clsSP sp;
		Double TongSL = 0.0;
		Double TongTien = 0.0;

		m_objSQL = new clsSQL(this, m_objDal);
		csr = m_objSQL.SanPham_GetByMaSP(m_MaSP);
		m_arlCollectDebt.clear();
		if (csr.moveToFirst()) {
			do {
				sp = new clsSP();
				sp.MaSP = csr.getString(csr.getColumnIndex("MaSP"));
				sp.TenSP = csr.getString(csr.getColumnIndex("TenSP"));
				sp.SoLuong = csr.getInt(csr.getColumnIndex("SL"));
				sp.DonGia = csr.getDouble(csr.getColumnIndex("DonGia"));
				sp.ThanhTien = csr.getDouble(csr.getColumnIndex("ThanhTien"));
				m_arlCollectDebt.add(sp);

				TongSL += sp.SoLuong;
				TongTien += sp.ThanhTien;

			} while (csr.moveToNext());
		} else {
			sp = new clsSP();
			sp.MaSP = "";
			sp.TenSP = "";
			sp.SoLuong = 0;
			sp.DonGia = 0.0;
			sp.ThanhTien = 0.0;
			m_arlCollectDebt.add(sp);
		}
		csr.close();

		m_TongSL.setText(clsUtil.GetDecimalFormat(TongSL));
		m_TongTien.setText(clsUtil.GetDecimalFormat(TongTien));

	}

	// Public sub class
	public static class clsSP {
		public String MaSP;
		public String TenSP;
		public int SoLuong;
		public Double DonGia;
		public Double ThanhTien;

	}

}
